package com.fanrui.code.util.core;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	/**
	 * 每天的毫秒数
	 * */
	public final static long DAY_MILLISECONDS = 86400000l;
	
	/**
	 * 每天的秒数
	 * */
	public final static long DAY_SECONDS = 86400l;
	
	/**
	 * 每天的分钟数
	 * */
	public final static long DAY_MINUTES = 1440l;
	
	/**
	 * 与当天某个时间的 时间差
	 * */
	public static long getTodayTimeDigitalSumVariation(String timeStr){
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		try {
			
			Calendar todayTargetTime = Calendar.getInstance();
			todayTargetTime.setTime(sdf.parse(timeStr));
			
			Calendar nowCalendar = Calendar.getInstance();
			nowCalendar.setTime(new Date());
			nowCalendar.set(Calendar.HOUR_OF_DAY,todayTargetTime.get(Calendar.HOUR_OF_DAY));
			nowCalendar.set(Calendar.MINUTE, todayTargetTime.get(Calendar.MINUTE));
			nowCalendar.set(Calendar.SECOND, todayTargetTime.get(Calendar.SECOND));
			return  nowCalendar.getTimeInMillis() - System.currentTimeMillis();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	/**
	 * 与下一天天某个时间的 时间差
	 * */
	public static long getNextdayTimeDigitalSumVariation(String timeStr){
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		try {
			
			Calendar todayTargetTime = Calendar.getInstance();
			todayTargetTime.setTime(sdf.parse(timeStr));
			
			Calendar nowCalendar = Calendar.getInstance();
			nowCalendar.setTime(new Date());
			nowCalendar.add(Calendar.DAY_OF_MONTH, 1);
			nowCalendar.set(Calendar.HOUR_OF_DAY,todayTargetTime.get(Calendar.HOUR_OF_DAY));
			nowCalendar.set(Calendar.MINUTE, todayTargetTime.get(Calendar.MINUTE));
			nowCalendar.set(Calendar.SECOND, todayTargetTime.get(Calendar.SECOND));
			return nowCalendar.getTimeInMillis() - System.currentTimeMillis() ;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	/**
	 * 获取当天零点的时间
	 */
	public static Date getZeroTime() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}
	
	/**
	 * 获取下一天零点
	 * */
	public static Date getTimeOf12() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.DAY_OF_MONTH, 1);
        return  cal.getTime();
    }
	
	/**
	 * 获取距离下一天零点的毫秒数
	 * */
	public static long getTimeOf12MilliSeconds(){
		return getTimeOf12().getTime()-System.currentTimeMillis();
	}
	
	
	/**
	 * 上一天的时间
	 * */
	public static Date getYesterday(){
		 Calendar cal = Calendar.getInstance();
	        cal.setTime(new Date());
	        cal.set(Calendar.HOUR_OF_DAY, 0);
	        cal.set(Calendar.MINUTE, 0);
	        cal.set(Calendar.SECOND, 0);
	        cal.set(Calendar.MILLISECOND, 0);
	        cal.add(Calendar.DAY_OF_MONTH, -1);
	        return  cal.getTime();
	}
	
	public static Date getYesterday(Date date){
		 Calendar cal = Calendar.getInstance();
	        cal.setTime(date);
	        cal.set(Calendar.HOUR_OF_DAY, 0);
	        cal.set(Calendar.MINUTE, 0);
	        cal.set(Calendar.SECOND, 0);
	        cal.set(Calendar.MILLISECOND, 0);
	        cal.add(Calendar.DAY_OF_MONTH, -1);
	        return  cal.getTime();
	}
	
	/**
	 * 上一个月
	 */
	public static Date getLastMonth() {
		Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.MONTH, -1);
        return  cal.getTime();
	}
	
	/**
	 * 年月日，时分秒
	 * @param time
	 * @return
	 */
	public static String getDateToString(Date time) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formatter.format(time);
	}
	
	/**
	 * 年月日
	 * @param time
	 * @return
	 */
	public static String getDateToStringDay(Date time) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		return formatter.format(time);
	}
}
